<template>
  <div>
    <el-card shadow="never">
      <h3>申请列表</h3>
    </el-card>

    <br>
    <el-card shadow="never">

      <el-table
        :data="tableData"
        style="width: 100%"
        border
      >
        <el-table-column
          label="申请人"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.user.nickName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="申请日期"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.createTime|formatDate2 }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="理由"
          align="center"
          width="100"
          type="expand"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.reason }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="申请结果"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.state === 0" type="warning">申请中</el-tag>
            <el-tag v-if="scope.row.state === 1" type="success">申请成功</el-tag>
            <el-tag v-if="scope.row.state === 2" type="danger">申请失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="申请处理"
          align="center"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.state === 0" size="mini" type="success" @click="updateState(scope.row, 1)">同意 </el-button>
            <el-button v-if="scope.row.state === 0" size="mini" type="danger" @click="updateState(scope.row, 2)">拒绝 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>

import * as applyApi from '@/api/apply'
import * as userApi from '@/api/user'
import { getUser } from '@/utils/auth'
export default {
  name: '',

  data() {
    return {
      addForm: {},
      tableData: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      applyApi.list().then(res => {
        this.tableData = res.data
      })
    },
    updateState(row, state) {
      row.state = state
      applyApi.update(row).then(resp => {
        this.$notifySuccess('操作成功')
        if (state === 1) {
          // 更新用户的角色
          const data = {
            id: row.userId,
            roles: '2'
          }
          userApi.updateUser(data)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
